
package com.guoyan.blockchain.common.user;


import com.guoyan.blockchain.common.client.CAClient;
import com.guoyan.blockchain.common.config.Config;

/**
 * @author Balaji Kadambi
 */

public class RegisterEnrollUser {

    public static void main(String[] args) {
        try {
//			Util.cleanUp();
            String caUrl = Config.CA_ORG2_URL;
            CAClient caClient = new CAClient(caUrl, null);
            // Enroll Admin to Org1MSP
            UserContext adminUserContext = new UserContext();
            adminUserContext.setName("admin");
            adminUserContext.setAffiliation("org1");
            adminUserContext.setMspId("Org1MSP");
            caClient.setAdminUserContext(adminUserContext);
            adminUserContext = caClient.enrollAdminUser(Config.ADMIN, Config.ADMIN_PASSWORD);
            if (adminUserContext.getRoles() != null) {
                adminUserContext.getRoles().forEach(System.out::println);
            } else {
                System.out.println(111);
            }

            // Register and Enroll user to Org1MSP
            UserContext userContext = new UserContext();
            String name = "user" + System.currentTimeMillis();
            userContext.setName(name);
            userContext.setAffiliation("org1");
            userContext.setMspId("Org1MSP");

            String eSecret = caClient.registerUser(name, Config.ORG1);

            userContext = caClient.enrollUser(userContext, eSecret);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
